package cn.lasse.springframework.aop.aspectj;

import cn.lasse.springframework.aop.Pointcut;
import cn.lasse.springframework.aop.PointcutAdvisor;
import lombok.Setter;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;

/**
 * 实现了 PointcutAdvisor 接口，把切面 pointcut、
 * 拦截方法 advice 和具体的拦截表达式包装在一起。
 * 这样就可以在 xml 的配置中定义一个 pointcutAdvisor 切面拦截器了
 * @author Lasse
 */
public class AspectJExpressionPointcutAdvisor implements PointcutAdvisor {
    /**
     * 切面点
     */
    private AspectJExpressionPointcut pointcut;
    /**
     * 具体的拦截方法
     */
    @Setter
    private Advice advice;
    /**
     * 表达式
     */
    @Setter
    private String expression;

    /**
     * 获取切入点
     */
    @Override
    public Pointcut getPointcut() {
        if (null == pointcut) {
            pointcut=new AspectJExpressionPointcut(expression);
        }
        return pointcut;
    }

    /**
     * Return the advice part of this aspect. An advice may be an
     * interceptor, a before advice, a throws advice, etc.
     *
     * @return the advice that should apply if the pointcut matches
     * @see MethodInterceptor
     */
    @Override
    public Advice getAdvice() {
        return advice;
    }
}